<?php

use Illuminate\Database\Seeder;
use App\Models\Shop;
use App\Models\User;
use App\Models\UserToShop;


class UserToShopTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $faker = app(Faker\Generator::class);

        $users = User::get();

        foreach ($users as $v) {
            $shops = Shop::inRandomOrder()->take($v->package->branch_num)->get()->pluck('id');
            $userToShop = factory(UserToShop::class)->times($v->package->branch_num)->make()->each(function($query , $index) use ($shops , $v){
                $query->user_id = $v->id;
                $query->shop_id = $shops[$index];
            });
            UserToShop::insert($userToShop->toArray());
        }


    }
}
